import { plantService } from "./plant.service.js";
import CModal from "@/components/utils/components/modal/modal.vue";
import checkUserPower from '../tools/checkUserPower.js';

export default {
    name: "APlant",
    data() {
        return {
            a: 0,
            currentPage3: 5,
            poetryList: [],
            isCheck: 0,
            allCheck: 0,
            modalShow: false,
            pageIndex: 1,
            pageSize: 10,
            pageCount: 0,
            isPower: true
        }
    },
    props: ["chooseClock"],
    components: {
        CModal
    },
    methods: {
        // 查询数据方法
        searchPoetryList() {
            if(this.chooseClock === "poetry") {
                plantService.search(this.pageIndex, this.pageSize)
                .then(res => {
                    this.pageCount = res.data.count;
                    this.poetryList = res.data.list;
                    console.log(res.data, this.pageCount);
                    console.log(this.pageIndex)
                })
            }else {
                console.log("choose other");
            }
        },
        // 添加数据方法
        addPoetryFn(data) {
            if(checkUserPower()) {
                this.$message({
                    type: 'warning',
                    message: '您无权限对此进行操作!'
                });
                return;
            }
            plantService.add(data.poetryTit, data.poetryMain)
            .then(res => {
                console.log(res);
                this.searchPoetryList();
                this.$refs.modalSwitch.closeModal();
            })
        },
        // 删除一条数据方法
        deleteOnce(id) {
            if(checkUserPower()) {
                this.$message({
                    type: 'warning',
                    message: '您无权限对此进行操作!'
                });
                return;
            }
            this.$confirm('确定删除本条信息吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                plantService.deleteOnce(id)
                .then(res => {
                    console.log(res);
                    this.searchPoetryList();
                })
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        // 删除多条信息方法
        deleteMany() {
            if(checkUserPower()) {
                this.$message({
                    type: 'warning',
                    message: '您无权限对此进行操作!'
                });
                return;
            }
            let data = [];
            for(let i = 0; i < this.poetryList.length; i++) {
                if(this.poetryList[i].isCheck === true)
                    data.push(this.poetryList[i]._id);
            }
            if(data.length > 0) {
                this.$confirm('确定删除选中信息吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    plantService.deleteMany(data)
                    .then(res => {
                        console.log(res);
                        this.searchPoetryList();
                    })
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            } else {
                this.$message({
                    type: 'info',
                    message: '没有需要删除的信息选项'
                }); 
            }
        },
        // 更新一条信息方法
        updataOnce(data) {
            if(checkUserPower()) {
                this.$message({
                    type: 'warning',
                    message: '您无权限对此进行操作!'
                });
                return;
            }
            plantService.updateOnce(data._id, data.poetryTit, data.poetryMain)
            .then(res => {
                console.log(res);
                this.searchPoetryList();
                this.$refs.modalSwitch.closeModal();
                // this.poetryList = res.data.list;
            })
        },
        handleSizeChange(val) {
            console.log(23);
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.pageIndex = val;
            this.searchPoetryList();
            console.log(`当前页: ${val}`);
        },
        // 单项checkbox按钮点击事件
        checkIsActive(index) {
            this.poetryList[index].isCheck === false ? this.poetryList[index].isCheck = true : this.poetryList[index].isCheck = false;
        },
        // checkbox全选按钮事件
        allCheckActive() {
            if (this.allCheck === 0) {
                for (let i = 0; i < this.poetryList.length; i++) {
                    this.poetryList[i].isCheck = true;
                }
            } else {
                for (let i = 0; i < this.poetryList.length; i++) {
                    this.poetryList[i].isCheck = false;
                }
            }
            this.allCheck === 0 ? this.allCheck = 1 : this.allCheck = 0;
        },
        // 打开modal事件
        openModal(type, data) {
            if(checkUserPower()) {
                this.$message({
                    type: 'warning',
                    message: '您无权限对此进行操作!'
                });
                return;
            }
            if(type === "alter") {
                this.$refs.modalSwitch.openModal(data);
            } else {
                this.$refs.modalSwitch.openModal(null);
            }
        },
    },
    mounted() {
        // this.isPower = checkUserPower();
    },
    created() {
        this.searchPoetryList();
    },
    filters: {
        transformDate: function (value) {
            let date = new Date(value);
            let y = 1900 + date.getYear();
            let m = "0" + (date.getMonth() + 1);
            let d = "0" + date.getDate();
            let h = "0" + date.getHours();
            let minute = "0" + date.getMinutes();
            let s = "0" + date.getSeconds();
            return y + "-" 
            + m.substring(m.length - 2, m.length) + "-" 
            + d.substring(d.length - 2, d.length) + " "
            + h.substring(h.length - 2, h.length) + ":"
            + minute.substring(minute.length - 2, minute.length) + ":"
            + s.substring(s.length - 2, s.length);
        }
    },
    watch: {
        chooseClock: function(newClock, oldClock) {
            this.searchPoetryList();
        }
    }
}